package leo.data
{
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import leo.manager.LoaderManager;

	/**
	 * xml配置文件转换 
	 * @author leo
	 * 
	 */
	public class Config
	{

		private var callBack :Function;
		public function Config(cb :Function = null)
		{
			this.callBack = cb;
			init()
		}
		
		private function init():void
		{
			var loader :URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE,loadConfigHandler);
			loader.load(new URLRequest(GameConst.CONFIG_URL));
		}
		
		private function loadConfigHandler(event :Event):void
		{
			var urlLoader :URLLoader = (event.target as URLLoader);
			var xml :XML = new XML(urlLoader.data);
			
			var mapObjAry :Array = GameModel.instance.imgList;
			if(xml.tower)
			{
				for each(var towerItem :XML in xml.tower.item)
				{
					TowerVO.create(towerItem);
				}
				
			}
			if(xml.monster)
			{
				for each(var monsterItem :XML in xml.monster.item)
				{
					mapObjAry.push(monsterItem.@source);
					MonsterVO.create(monsterItem);
				}
			}
			
			if(xml.ui)
			{
				for each(var uiItem :XML in xml.ui.item)
				{
					mapObjAry.push(uiItem.@source);
				}
			}
			
			if(xml.map)
			{
				var mapAry :Array = GameModel.instance.mapAry;
				for each(var mapItem :XML in xml.map.item)
				{
					var str :String = mapItem.@src;
					mapAry.push(str);
				}
			}
			
			if(xml.config)
			{
				GameConfig.big_loading_url = xml.config.big_loading_url.@value;
				GameConfig.map_name_ary = strToAry(xml.config.map_name_ary.@value);
				GameConfig.mc_tower_ary = strToAry(xml.config.mc_tower_ary.@value);
				GameConfig.mc_bullet_ary = strToAry(xml.config.mc_bullet_ary.@value);
				GameConfig.is_show_grid = xml.config.is_show_grid.@value == "true" ? true : false;
				GameConfig.monster_list = strToAry(xml.config.monster_list.@value);
				GameConfig.m_start_point_ary = this.str2ary(xml.config.m_start_point_ary.@value);
				GameConfig.m_end_point_ary = this.str2ary(xml.config.m_end_point_ary.@value);
				GameConfig.drop_speed_odds = xml.config.drop_speed_odds.@value;
				GameConfig.drop_speed = xml.config.drop_speed.@value;
				GameConfig.drop_speed_time = xml.config.drop_speed_time.@value;
				GameConfig.create_monster_time = xml.config.create_monster_time.@value;
				GameConfig.def_gold = xml.config.def_gold.@value;
				GameConfig.level_monster_num = xml.config.level_monster_num.@value;
			}
			(event.target as URLLoader).removeEventListener(Event.COMPLETE,loadConfigHandler);
			
			if(this.callBack != null)
			{
				this.callBack();
			}
			if(!mapObjAry || mapObjAry.length == 0)
			{
				trace("Config.mapObjAry null return");
				return;
			}
		}
		/**
		 * 字符串转成起点终点数组 
		 * @param str
		 * @return 
		 * 
		 */		
		private function str2ary(str :String):Array
		{
			var ary :Array = [];
			var tempAry :Array = str.split(",");
			var len :int = tempAry.length/2;
			for(var i :int = 0; i < len; i++)
			{
				var p :Point = new Point(tempAry.shift(),tempAry.shift());
				ary.push(p);
			}
			return ary;
		}
		/**
		 * 字符串转数组 
		 * @param str
		 * @return 
		 * 
		 */		
		private function strToAry(str :String):Array
		{
			return  str.split(",");
		}
	}
}